﻿using System.Collections.Generic;
using System.Linq;
using Apps.Web.Core;
using Apps.IBLL.LMS;
using Apps.Locale;
using System.Web.Mvc;
using Apps.Common;
using Apps.IBLL;
using Microsoft.Practices.Unity;
using Apps.Models;
using Apps.BLL.SelfRcRelated;
using Apps.Models.CustomModels;
using Apps.BLL.YANX;
using DocumentFormat.OpenXml.Office2010.Excel;
using Apps.BLL.LMS;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;

namespace Apps.Web.Areas.LMS.Controllers
{
    public class TestDataReportController : Controller
    {
        SelfRcRelatedBLL bll = new SelfRcRelatedBLL();
        YANX_RECORD_BLL bllYx = new YANX_RECORD_BLL();
        ValidationErrors errors = new ValidationErrors();
        LMS_TESTDATA_AUDITBLL m_testdataBLL = new LMS_TESTDATA_AUDITBLL();

        [SupportFilter]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [SupportFilter(ActionName = "Index")]
        public JsonResult GetList()
        {
            int totalCount = 0;
            List<TestDataOutPutModel> list = m_testdataBLL.GetSugarList(new Models.CustomModels.TestDataQuertParaModel
            {
                BatchType = Utils.ObjectToStr(Request["BatchType"]),
                BatchCode = Utils.ObjectToStr(Request["BatchCode"]),
                MaterielName = Utils.ObjectToStr(Request["MaterielName"]),
                PageIndex = Utils.ObjToInt(Request["page"], 1),
                PageSize = Utils.ObjToInt(Request["rows"], 15),
                StartMatchTime = Utils.ObjectToStr(Request["StartDate"]),
                EndMatchTime = Utils.ObjectToStr(Request["EndDate"]),
                TypeStr = Utils.ObjectToStr(Request["TypeStr"]),
                ISJSUAN = Utils.ObjectToStr(Request["isJsuan"]),
                ISRELATED = Utils.ObjectToStr(Request["jsRelated"]),
                ApprovalStatus = 3,
                OrderDate= "MATCH_TIME",
            }, ref totalCount);
            if (list != null)
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        list[i].BATCH_TYPE = list[i].BATCH_TYPE == "自采煤" ? "其他" : list[i].BATCH_TYPE;
                        list[i].STR_MATCH_TIME = list[i].MATCH_TIME.ToString("yyyy-MM-dd");
                        list[i].STR_COLLECT_TIME = list[i].COLLECT_TIME.ToString("yyyy-MM-dd HH:mm:ss");
                        list[i].CSR = !string.IsNullOrEmpty(list[i].SELF_CSR) ? Utils.ToOnePoint(list[i].SELF_CSR) : Utils.ToOnePoint(list[i].CSR);
                        list[i].CRI = !string.IsNullOrEmpty(list[i].SELF_CRI) ? Utils.ToOnePoint(list[i].SELF_CRI) : Utils.ToOnePoint(list[i].CRI);
                        list[i].YANX_VALUE = !string.IsNullOrEmpty(list[i].SELF_YANX_VALUE) ? list[i].SELF_YANX_VALUE : list[i].YANX_VALUE;
                        if (!string.IsNullOrEmpty(list[i].XJLM_CODE) || !string.IsNullOrEmpty(list[i].XJLJT_CODE))
                            list[i].SELF_BATCH_CODE = list[i].XJLM_CODE + "," + list[i].XJLJT_CODE;
                    }
                    ViewBag.ID = list[0].AUDIT_ID;
                }
            return Json(new Pagination.PaginationResult<TestDataOutPutModel>(totalCount, list == null ? new List<TestDataOutPutModel>() : list.ToList()));
        }

        /// <summary>
        /// 导出 
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        [HttpGet]
        [SupportFilter(ActionName = "Index")]
        public FileResult Export(string MaterielName, string TypeStr, string StartDate, string EndDate, string BatchType, string ISJSUAN, string jsRelated ,string batchCode)
        {
            int totalCount = 0;
            List<TestDataOutPutModel> list = new List<TestDataOutPutModel>();
            list = m_testdataBLL.GetSugarList(new Models.CustomModels.TestDataQuertParaModel
            {
                BatchCode= batchCode,
                BatchType = BatchType,
                EndMatchTime = EndDate,
                MaterielName = MaterielName,
                StartMatchTime = StartDate,
                TypeStr = TypeStr,
                ApprovalStatus = 3,
                ISJSUAN = ISJSUAN,
                ISRELATED = jsRelated,
                OrderDate = "MATCH_TIME",
            }, ref totalCount, false);
            if (list != null)
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        list[i].BATCH_TYPE = list[i].BATCH_TYPE == "自采煤" ? "其他" : list[i].BATCH_TYPE;
                        list[i].STR_COLLECT_TIME = list[i].COLLECT_TIME.ToString("yyyy-MM-dd HH:mm:ss");
                        list[i].STR_MATCH_TIME = list[i].MATCH_TIME.ToString("yyyy-MM-dd");
                        list[i].CSR = !string.IsNullOrEmpty(list[i].SELF_CSR) ? Utils.ToOnePoint(list[i].SELF_CSR) : Utils.ToOnePoint(list[i].CSR);
                        list[i].CRI = !string.IsNullOrEmpty(list[i].SELF_CRI) ? Utils.ToOnePoint(list[i].SELF_CRI) : Utils.ToOnePoint(list[i].CRI);
                        list[i].YANX_VALUE = !string.IsNullOrEmpty(list[i].SELF_YANX_VALUE) ? list[i].SELF_YANX_VALUE : list[i].YANX_VALUE;
                        if (!string.IsNullOrEmpty(list[i].XJLM_CODE) || !string.IsNullOrEmpty(list[i].XJLJT_CODE))
                            list[i].SELF_BATCH_CODE = list[i].XJLM_CODE + "," + list[i].XJLJT_CODE;
                    }
                }
            if (list.Count > 0)
            {
                HSSFWorkbook hssfworkbook = new HSSFWorkbook();
                #region 

                IFont font = hssfworkbook.CreateFont();
                font.FontName = "宋体";
                font.FontHeightInPoints = 11;
                //表头样式
                ICellStyle style = hssfworkbook.CreateCellStyle();
                //设置单元格边框
                style.SetFont(font);

                ICellStyle styleRight = hssfworkbook.CreateCellStyle();
                styleRight.Alignment = HorizontalAlignment.Right;
                //设置单元格边框
                styleRight.SetFont(font);

                #endregion

                ISheet sheet1 = null;
                IRow rowHeader;
                int index = 0;
                int rowIndex = 0;
                int colIndex = 0;
                sheet1 = hssfworkbook.CreateSheet($"化验数据台账");
                rowHeader = sheet1.CreateRow(rowIndex);
                rowHeader.CreateCell(colIndex++).SetCellValue("收样时间");
                rowHeader.CreateCell(colIndex++).SetCellValue("批次类型");
                rowHeader.CreateCell(colIndex++).SetCellValue("批次码");
                rowHeader.CreateCell(colIndex++).SetCellValue("物料");
                rowHeader.CreateCell(colIndex++).SetCellValue("类型");
                rowHeader.CreateCell(colIndex++).SetCellValue("CRI");
                rowHeader.CreateCell(colIndex++).SetCellValue("CSR");
                rowHeader.CreateCell(colIndex++).SetCellValue("岩相");
                rowHeader.CreateCell(colIndex++).SetCellValue("Mt");
                rowHeader.CreateCell(colIndex++).SetCellValue("Mad");
                rowHeader.CreateCell(colIndex++).SetCellValue("Ad");
                rowHeader.CreateCell(colIndex++).SetCellValue("Vdaf");
                rowHeader.CreateCell(colIndex++).SetCellValue("St,d");
                rowHeader.CreateCell(colIndex++).SetCellValue("G");
                rowHeader.CreateCell(colIndex++).SetCellValue("Y");
                rowHeader.CreateCell(colIndex++).SetCellValue("X");
                rowHeader.CreateCell(colIndex++).SetCellValue("a");
                rowHeader.CreateCell(colIndex++).SetCellValue("b");
                rowHeader.CreateCell(colIndex++).SetCellValue("Qnet,ar（kcal/kg)");
                rowHeader.CreateCell(colIndex++).SetCellValue("<3mm");
                rowHeader.CreateCell(colIndex++).SetCellValue("3~10mm");
                rowHeader.CreateCell(colIndex++).SetCellValue(">10mm");
                rowHeader.CreateCell(colIndex++).SetCellValue("M40");
                rowHeader.CreateCell(colIndex++).SetCellValue("M10");
                rowHeader.CreateCell(colIndex++).SetCellValue(">80mm");
                rowHeader.CreateCell(colIndex++).SetCellValue("80~60mm");
                rowHeader.CreateCell(colIndex++).SetCellValue("60~40mm");
                rowHeader.CreateCell(colIndex++).SetCellValue("40~25mm");
                rowHeader.CreateCell(colIndex++).SetCellValue("<25mm");

                list.ForEach(res =>
                {
                    rowIndex++;
                    index++;
                    colIndex = 0;
                    rowHeader = sheet1.CreateRow(rowIndex);
                    rowHeader.CreateCell(colIndex++).SetCellValue(res.STR_COLLECT_TIME);
                    rowHeader.CreateCell(colIndex++).SetCellValue(res.BATCH_TYPE);
                    rowHeader.CreateCell(colIndex++).SetCellValue(res.BATCH_CODE);
                    rowHeader.CreateCell(colIndex++).SetCellValue(res.MATERIEL_NAME);
                    rowHeader.CreateCell(colIndex++).SetCellValue(res.TYPE_STR);
                    if (Utils.ToDouble(res.CRI) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.CRI));

                    if (Utils.ToDouble(res.CSR) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.CSR));
                    if (Utils.ToDouble(res.YANX_VALUE) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.YANX_VALUE));

                    if (Utils.ToDouble(res.MT) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.MT));

                    if (Utils.ToDouble(res.MAD) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.MAD));

                    if (Utils.ToDouble(res.AD) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.AD));

                    if (Utils.ToDouble(res.VDAF) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.VDAF));

                    if (Utils.ToDouble(res.STD) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.STD));

                    if (Utils.ToDouble(res.G) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.G));

                    if (Utils.ToDouble(res.Y) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.Y));

                    if (Utils.ToDouble(res.X) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.X));

                    if (Utils.ToDouble(res.A) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.A));

                    if (Utils.ToDouble(res.B) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.B));

                    if (Utils.ToDouble(res.QNETAR_KCAL) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.QNETAR_KCAL));

                    if (Utils.ToDouble(res.SMALL) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.SMALL));

                    if (Utils.ToDouble(res.SMALL) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.MEDIUM));

                    if (Utils.ToDouble(res.LARGE) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.LARGE));

                    if (Utils.ToDouble(res.M40) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.M40));

                    if (Utils.ToDouble(res.M10) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.M10));

                    if (Utils.ToDouble(res.JTXXLARGE) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.JTXXLARGE));

                    if (Utils.ToDouble(res.JTXLARGE) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.JTXLARGE));

                    if (Utils.ToDouble(res.JTLARGE) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.JTLARGE));

                    if (Utils.ToDouble(res.JTMEDIUM) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.JTMEDIUM));

                    if (Utils.ToDouble(res.JTSMALL) == 0)
                        rowHeader.CreateCell(colIndex++).SetCellValue("");
                    else
                        rowHeader.CreateCell(colIndex++).SetCellValue(Utils.ToDouble(res.JTSMALL));

                });

                MemoryStream file = new MemoryStream();
                hssfworkbook.Write(file);
                file.Seek(0, SeekOrigin.Begin);
                byte[] excelbyte = file.ToArray();
                return File(excelbyte, @"application/ms-excel", $"化验数据台账 日期：({StartDate}-{EndDate}).xls");
            }
            return null;
        }



    }
}
